package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.StorePageQueryDTO;
import net.wanho.po.Store;
import net.wanho.service.StoreService;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/store")
public class StoreController {

    private final StoreService storeService;

    // 查询仓库信息
    @GetMapping("/store-list")
    public R storeList() {
        List<Store> list = storeService.list();
        return R.ok(list);
    }

    // 分页查询仓库信息
    @GetMapping("/store-page-list")
    public R getStorePage(@RequestParam(required = false, defaultValue = "1") int pageNum,
                          @RequestParam(required = false, defaultValue = "5") int pageSize,
                          Store store) {
        PageInfo<Store> page = storeService.getStorePage(pageNum, pageSize, store);
        return R.ok(page);
    }

    // 检查仓库是否唯一
    @GetMapping("/store-num-check")
    public R checkStoreNum(Integer storeNum) {
        Store one = storeService.selectOneByStoreNum(storeNum);
        if (one != null) {
            return R.error(false);
        }
        return R.ok(true);
    }

    // 添加仓库
    @PostMapping("/store-add")
    public R addStore(@RequestBody Store store) {
        boolean save = storeService.save(store);
        if (save) {
            return R.ok("仓库添加成功！");
        }
        return R.error("添加失败");
    }

    // 修改仓库
    @PutMapping("/store-update")
    public R updateStore(@RequestBody Store store) {
        boolean update = storeService.updateById(store);
        if (update) {
            return R.ok("仓库修改成功！");
        }
        return R.error("修改失败");
    }

    // 删除仓库
    @DeleteMapping("/store-delete/{id}")
    public R deleteStore(@PathVariable Integer id) {
        boolean delete = storeService.removeById(id);
        if (delete) {
            return R.ok("仓库删除成功！");
        }
        return R.error("删除失败");
    }

    // 导出数据
    @GetMapping("/exportTable")
    public R exportTable(StorePageQueryDTO store) {
        List<Store> list = storeService.exportTable(store);
        return R.ok("导出成功", list);
    }

}